//gcc main.c -o lichuan  -L ./  -lethercat
#include <ecrt.h>
#include <stdio.h>
#include <signal.h>
#include <sys/time.h>
/* Master 0, Slave 0
 * Vendor ID:       0x00000a79
 * Product code:    0x00001000
 * Revision number: 0x00000001
 */
static unsigned int  cntlwd;
static unsigned int  status;
static ec_master_t *master = NULL;
static ec_domain_t *domain = NULL;
static ec_slave_config_t *sc_lichuan;
static uint8_t *domain_pd = NULL;
#define pos0 0,0
#define lichuan 0x00000a79,0x00001000
ec_pdo_entry_info_t slave_0_pdo_entries[] = {
    {0x603f, 0x00, 16}, /* ErrorCode */
    {0x6041, 0x00, 16}, /* StatusWord */
    {0x6064, 0x00, 32}, /* Position actual value */
    {0x60fd, 0x00, 32}, /* Digital inputs */
    {0x6061, 0x00, 8}, /* Modes of operation display */
    {0x6040, 0x00, 16}, /* ControlWord */
    {0x6060, 0x00, 8}, /* Modes of operation */
    {0x607a, 0x00, 32}, /* Target position */
    {0x6081, 0x00, 32}, /* Profile velocity */
    {0x6099, 0x01, 32}, /* Search Switch */
    {0x6099, 0x02, 32}, /* Search Zero */
    {0x6040, 0x00, 16},
    {0x607a, 0x00, 32},
    {0x60ff, 0x00, 32},
    {0x6060, 0x00, 8},
    {0x0000, 0x00, 8}, /* Gap */
    {0x6041, 0x00, 16},
    {0x6064, 0x00, 32},
    {0x606c, 0x00, 32},
    {0x6061, 0x00, 8},
    {0x0000, 0x00, 8}, /* Gap */
};

ec_pdo_info_t slave_0_pdos[] = {
    {0x1a01, 5, slave_0_pdo_entries + 0}, /* TxPDO 2 */
    {0x1601, 6, slave_0_pdo_entries + 5}, /* RxPDO 2 */
    {0x1600, 5, slave_0_pdo_entries + 11},
    {0x1a00, 5, slave_0_pdo_entries + 16},
};

ec_sync_info_t lichuan_syncs[] = {
    {0, EC_DIR_OUTPUT, 2, slave_0_pdos + 0, EC_WD_DISABLE},
    {1, EC_DIR_INPUT, 0, NULL, EC_WD_DISABLE},
    {2, EC_DIR_OUTPUT, 1, slave_0_pdos + 2, EC_WD_ENABLE},
    {3, EC_DIR_INPUT, 1, slave_0_pdos + 3, EC_WD_DISABLE},
    {0xff}
};
ec_pdo_entry_reg_t domain_regs[] = {
    {pos0, lichuan, 0x6040, 0x00, &cntlwd, NULL},
    {pos0, lichuan, 0x6041, 0x00, &status, NULL},
    {}
};

void set_timer(){
        struct itimerval itv;
        itv.it_value.tv_sec = 1;
        itv.it_value.tv_usec = 50000;
        itv.it_interval.tv_sec = 0;
        itv.it_interval.tv_usec = 6000;
        setitimer(ITIMER_REAL, &itv, NULL);
}

void signal_handler(int m){
        ecrt_master_receive(master);
        ecrt_domain_process(domain);
        ecrt_master_send(master);
        printf("cylcic time is 60ms\n");
}
 
int main(void)
{
    int conw;
    master = ecrt_request_master(0);
    if(!master)
        return 1;
    sc_lichuan=ecrt_master_slave_config(master,pos0,lichuan);
    ecrt_slave_config_pdos(sc_lichuan,EC_END,lichuan_syncs);
    domain = ecrt_master_create_domain(master);
    ecrt_domain_reg_pdo_entry_list(domain, domain_regs);
    ecrt_master_activate(master);
    domain_pd = ecrt_domain_data(domain);
    printf("Activating master...success\n");
    signal(SIGALRM, signal_handler);
    set_timer();
    while(1){
        printf( "Enter control word :");
        scanf("%d",&conw);
        EC_WRITE_U16(domain_pd+cntlwd,conw);
        sleep(1);
        printf("servo status is %d",EC_READ_U16(domain_pd+status));
    }
    ecrt_release_master(master);
    return 0;
}